package ext.mango.aa.po;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ext.mango.aa.eo.EOAAMenu;
import ext.mango.aa.eo.EOAAMenuToUserGroup;
import ext.mango.base.Messages;
import ext.mango.base.POBase;
import mango.functions.FuncString;
import mango.orm.DaoFactory;
import mango.orm.DBSession;
import mango.orm.DBSessionFactory;
import mango.orm.entity.Entity;
import mango.web.Info;

public class MenuManager extends POBase {
    public boolean readmenu() {
        String menuIdOrName = this.getRequestParameter("MENU"); //$NON-NLS-1$
        String groupId = this.getRequestParameter("GROUPID"); //$NON-NLS-1$
        String sql = "select ID QUERYID, TITLE, ID, PARENT, '' PTITLE, LINK, FUNC_AA_GETMENUGROUPS(id) GROUPIDS, ICONCLS, MENUORDER, LEAF from AAMenu where 1=1 "; //$NON-NLS-1$

        if (!FuncString.isBlank(menuIdOrName)) {
            sql += " and (id=@id or parent=@parent or title like @title)"; //$NON-NLS-1$
        }
        if (!FuncString.isBlank(groupId)) {
            if (!"Super".equals(groupId)) { //$NON-NLS-1$
                sql += " and exists(select 1 from AAMenuToUserGroup where usergroupid=@groupid and menuid=AAMenu.id)"; //$NON-NLS-1$
            }
        }
        
        List<Entity> ret = DaoFactory.c(sql).setParamByName("id", menuIdOrName) //$NON-NLS-1$
                .setParamByName("parent", menuIdOrName) //$NON-NLS-1$
                .setParamByName("title", "%" + menuIdOrName + "%") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                .setParamByName("groupid", groupId).listQuery(); //$NON-NLS-1$

        this.setResult(Entity.toJSON(ret, "menu")); //$NON-NLS-1$
        return true;
    }

    public boolean updateMenu() {
        String TITLE = this.getRequestParameter("TITLE"); //$NON-NLS-1$
        String QUERYID = this.getRequestParameter("QUERYID"); //$NON-NLS-1$
        String ID = this.getRequestParameter("ID"); //$NON-NLS-1$
        String PARENT = this.getRequestParameter("PARENT"); //$NON-NLS-1$
        String LINK = this.getRequestParameter("LINK"); //$NON-NLS-1$
        String GROUPIDS = this.getRequestParameter("GROUPIDS"); //$NON-NLS-1$
        String ICONCLS = this.getRequestParameter("ICONCLS"); //$NON-NLS-1$
        String MENUORDER = this.getRequestParameter("MENUORDER"); //$NON-NLS-1$
        String LEAF = this.getRequestParameter("LEAF"); //$NON-NLS-1$
        // 新建
        if (FuncString.isBlank(QUERYID)) {
            EOAAMenu menu = new EOAAMenu().setTitle(TITLE).setId(ID).setParent(PARENT)
                    .setLink(LINK).setIconCls(ICONCLS).setMenuOrder(Integer.parseInt(MENUORDER))
                    .setMakedate(new Date()).setLeaf(Boolean.parseBoolean(LEAF) ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$
            DBSession session = DBSessionFactory.getCurrentSession();
            session.save(menu);
            if(!FuncString.isBlank(GROUPIDS)){
                String[] groups = GROUPIDS.split(","); //$NON-NLS-1$
                List<Entity> list = new ArrayList<Entity>();
                for (String group : groups) {
                    if(!FuncString.isBlank(group)){
                        EOAAMenuToUserGroup eo = new EOAAMenuToUserGroup().setUserGroupId(group.trim())
                        .setMenuId(ID);
                        list.add(eo);
                    }
                }
                session.save(list);
            }
            session.commit();
            this.addInfo(Info.SUCCESS, ID);
        }
        else {
            // 更新
            EOAAMenu menu = new EOAAMenu().setTitle(TITLE).setId(ID).setParent(PARENT)
                    .setLink(LINK).setIconCls(ICONCLS).setMenuOrder(Integer.parseInt(MENUORDER))
                    .setMakedate(new Date()).setLeaf(Boolean.parseBoolean(LEAF) ? "1" : "0"); //$NON-NLS-1$ //$NON-NLS-2$
            DBSession session = DBSessionFactory.getCurrentSession();
            String sql = "delete from AAMenuToUserGroup where menuid=?"; //$NON-NLS-1$
            session.execute(sql, ID);
            if(!FuncString.isBlank(GROUPIDS)){
                String[] groups = GROUPIDS.split(","); //$NON-NLS-1$
                List<Entity> list = new ArrayList<Entity>();
                for (String group : groups) {
                    if(!FuncString.isBlank(group)){
                        EOAAMenuToUserGroup eo = new EOAAMenuToUserGroup().setUserGroupId(group.trim())
                            .setMenuId(ID);
                        list.add(eo);
                    }
                }
                session.save(list);
            }
            session.update(menu);
            session.commit();
        }
        return true;
    }
    
    public boolean deleteMenu(){
        String id = this.getRequestParameter("ID"); //$NON-NLS-1$
        String sql = "select 1 from AAMenuToUserGroup where menuid=? and usergroupid='Super'"; //$NON-NLS-1$
        String ret = DaoFactory.c(sql).setParameter(id).getStringValue();
        if("1".equals(ret)){ //$NON-NLS-1$
            this.addInfo(Info.ERROR, Messages.getString(this.getLanguage(),"supermenucantbedelete")); //$NON-NLS-1$
            return false;
        }
        String sql1 = "delete from AAMenu where id=?"; //$NON-NLS-1$
        String sql2 = "delete from AAMenuToUserGroup where menuId=?"; //$NON-NLS-1$
        DBSession session = DBSessionFactory.getCurrentSession();
        session.execute(sql1, id);
        session.execute(sql2, id);
        session.commit();
        this.addInfo(Info.SUCCESS, "OK"); //$NON-NLS-1$
        return true;
    }
}
